package com.my.vote.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.my.vote.entity.VoteAnswer;
import com.my.vote.entity.VoteSubject;
import com.my.vote.repository.VoteAnswerRepository;
import com.my.vote.repository.VoteSubjectRepository;
import com.my.vote.repository.param.VoteAnswerSVO;
import com.sskj.core.annotation.ControllerLogExeTime;

@RestController
@RequestMapping("/vote/answer")
public class VoteAnswerServiceImpl {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	VoteAnswerRepository voteAnswerRepository;

	@Autowired
	VoteSubjectRepository voteSubjectRepository;

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/one", method = { RequestMethod.GET })
	@ControllerLogExeTime(description = "查询")
	public VoteAnswer findOne(String id) {
		return voteAnswerRepository.findOne(id);
	};

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/all/by/subject", method = { RequestMethod.GET })
	@ControllerLogExeTime(description = "查询问题全部答案")
	public List<VoteAnswerSVO> findAllBySubjectId(String subjectId, String voterId) {
		List<VoteAnswer> vas = voteAnswerRepository.findAllBySubjectId(subjectId, voterId);
		List<VoteAnswerSVO> vaSVOs = new ArrayList<VoteAnswerSVO>();
		if (vas != null && vas.size() > 0) {
			for (VoteAnswer va : vas) {
				VoteAnswerSVO vaSvo = new VoteAnswerSVO();
				vaSvo.setId(va.getId());
				vaSvo.setAnswer(va.getAnswer());
				vaSvo.setQuestionId(va.getQuestionId());
				vaSvo.setSubjectId(va.getSubjectId());
				vaSvo.setVoterId(va.getVoterId());
				vaSvo.setVoteTime(va.getVoteTime());
				vaSVOs.add(vaSvo);
			}
		}
		logger.debug(JSON.toJSONString(vaSVOs));
		return vaSVOs;
	};

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/is/voted", method = { RequestMethod.GET })
	@ControllerLogExeTime(description = "查询是否已经投票过")
	public boolean isVoted(String subjectId, String voterId) {
		List<VoteAnswer> vas = voteAnswerRepository.findAllBySubjectId(subjectId, voterId);
		return vas.size() > 0;
	};

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/all/by/question", method = { RequestMethod.GET })
	@ControllerLogExeTime(description = "查询问题全部答案")
	public List<VoteAnswerSVO> findAllByQuestionId(String questionId, String voterId) {
		List<VoteAnswer> vas = voteAnswerRepository.findAllByQuestionId(questionId, voterId);
		List<VoteAnswerSVO> vaSVOs = new ArrayList<VoteAnswerSVO>();
		if (vas != null && vas.size() > 0) {
			for (VoteAnswer va : vas) {
				VoteAnswerSVO vaSvo = new VoteAnswerSVO();
				vaSvo.setId(va.getId());
				vaSvo.setAnswer(va.getAnswer());
				vaSvo.setQuestionId(va.getQuestionId());
				vaSvo.setSubjectId(va.getSubjectId());
				vaSvo.setVoterId(va.getVoterId());
				vaSvo.setVoteTime(va.getVoteTime());
				vaSVOs.add(vaSvo);
			}
		}
		logger.debug(JSON.toJSONString(vaSVOs));
		return vaSVOs;
	};

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/subject/question/option/id", method = { RequestMethod.GET })
	@ControllerLogExeTime(description = "根据问卷id，问题id，选项id查询问题答案")
	public List<VoteAnswerSVO> findBySubjectAndQuestionAndOptionId(String subjectId, String questionId, String optionId,
			String voterId) {
		List<VoteAnswer> vas = voteAnswerRepository.findBySubjectAndQuestionAndOptionId(subjectId, questionId, optionId,
				voterId);
		List<VoteAnswerSVO> vaSVOs = new ArrayList<VoteAnswerSVO>();
		if (vas != null && vas.size() > 0) {
			for (VoteAnswer va : vas) {
				VoteAnswerSVO vaSvo = new VoteAnswerSVO();
				vaSvo.setId(va.getId());
				vaSvo.setAnswer(va.getAnswer());
				vaSvo.setQuestionId(va.getQuestionId());
				vaSvo.setSubjectId(va.getSubjectId());
				vaSvo.setVoterId(va.getVoterId());
				vaSvo.setVoteTime(va.getVoteTime());
				vaSVOs.add(vaSvo);
			}
		}
		logger.debug(JSON.toJSONString(vaSVOs));
		return vaSVOs;
	};

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/save", method = { RequestMethod.POST })
	@ControllerLogExeTime(description = "保存问卷调查结构")
	public boolean save(String answer) {
		logger.debug(answer);
		try {
			List<VoteAnswer> vas = JSON.parseArray(answer, VoteAnswer.class);
			logger.debug(JSON.toJSONString(vas));
			Date now = new Date();
			for (VoteAnswer va : vas) {
				va.setCreatorId(va.getVoterId());
				va.setVoteTime(now);
				voteAnswerRepository.save(va);
			}
			if (vas != null && vas.size() > 0) {
				VoteAnswer va = vas.get(0);
				VoteSubject vs = voteSubjectRepository.findOne(va.getSubjectId());
				if (vs != null) {
					int count = vs.getVoteCount();
					vs.setVoteCount(count + 1);
					voteSubjectRepository.save(vs);
				}
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/vote/count", method = { RequestMethod.GET })
	@ControllerLogExeTime(description = "问卷答题人数统计")
	public int voteCount(String subjectId) {
		return voteAnswerRepository.voteCount(subjectId);
	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/vote/count/option", method = { RequestMethod.GET })
	@ControllerLogExeTime(description = "问卷问题选项被选择数量")
	public int voteCountOption(String optionId) {
		return voteAnswerRepository.voteCountOption(optionId);
	}

}
